package cn.u313.comm.utils;


import cn.u313.config.BotConfig;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.message.events.PrivateMsg;
import love.forte.simbot.api.sender.MsgSender;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;

import static cn.u313.comm.conststring.BiaoQing.bq;

/**
 * 消息工具类
 */
public class MsgUtil {
    public  static String ADMSG = "\n✨✨✨✨✨✨✨✨\n" +
            "[CAT:face,id=66]看看谁回来啦\n" +
            "[CAT:face,id=66]原来是可爱的我呀";
    public static  String preStr=null;
    public static  int n=95;
    public static  int maxToken = 500;
    public static Map<String, GroupMsg> qun = new HashMap<>();
    private String catCodeImg = "[CAT:image,url=%s,flash=%s]";
    private String catCodeFileImg = "[CAT:image,file=%s,flash=%s]";
    public String getImgCAT(String img,boolean bool){
        if (img.contains("http"))
            return String.format(catCodeImg,img,bool) ;
        return String.format(catCodeFileImg,img,bool) ;
    }
    public String getImgCAT(String img){
        return getImgCAT(img,false) ;
    }
    private String appendImg(String txt){
//        if(ADMSG.)
//        ADMSG ="\n---------------------\n"+ TxtzhuFU.新年祝福语();
//        if(1==1){
//            return txt + ADMSG + ImgDB.随机新年表情();
//        }
        if(true){

            return txt + ADMSG;

//            return txt + ADMSG + getImgCAT(TxtUtil.radomTxt(bq),false) ;
        }
        String imgPath = "image/"+(int)(Math.random()*n)+"";
//        String imgPath = ImgDB.随机新年表情();
        try {
            File file = new File(imgPath+".jpg");
//            File file = new File(imgPath);
            if(file==null)
                imgPath = imgPath+".gif";
            else
                imgPath = imgPath+".jpg";

        } catch (Exception e) {

            imgPath = imgPath+".gif";
        }

        if(ADMSG.contains("image"))
            return txt + ADMSG ;

        return txt + ADMSG + getImgCAT(imgPath,false) ;
    }

    private  static String getAD(){
        return "";
    }
    public static void sendMsg(MsgSender msgSender, GroupMsg groupMsg, String s){
        cloerMsg(groupMsg);
        if(s==null)return;
//        if(Util.getTime(0)>=20220602){
//            sendMsg(msgSender,groupMsg,s+"\n\n端午安康"+"[CAT:image,imageType=JPG,isEmoji=false,url="+TxtUtil.radomTxt(ImgDB.端午表情包)+"]",false);
//            return;
//        }

        if(!s.contains("xml")&&!s.contains("CAT:image")&&!s.contains("content")){
            if(Util.getTime(0) ==20220521)
                s="[CAT:face,id=66]"+ s.replaceAll("\\n","\n[CAT:face,id=66]");
            s = new MsgUtil().appendImg(s);
        }
        try {
            System.err.println(s);
            msgSender.SENDER.sendGroupMsg(groupMsg,s);
        } catch (Exception e) {
            e.printStackTrace();

            System.err.println(s);
            msgSender.SENDER.sendGroupMsg(groupMsg,s.replace("jpg","gif"));
        }
    }

    public static void sendPrivateMsg(MsgSender msgSender, PrivateMsg groupMsg, String s){

        msgSender.SENDER.sendPrivateMsg(groupMsg,new MsgUtil().appendImg(s));
    }
//    public static void sendMsg(MsgSender msgSender, GroupMsgDelete groupMsg, String s){
////        cloerMsg(groupMsg);
//        msgSender.SENDER.sendGroupMsg(groupMsg,s);
//    }

    /**
     * 清空 当前群
     * @param groupMsg
     */
    private static void cloerMsg(GroupMsg groupMsg){
        if(qun.get(groupMsg.getGroupInfo().getGroupCode())!=null){
            qun.replace(groupMsg.getGroupInfo().getGroupCode(),null);
        }

    }
    /**
     * 设置是否复读？
     * @param groupMsg
     */
    public static boolean isFuMsg(GroupMsg groupMsg){
        GroupMsg m=qun.get(groupMsg.getGroupInfo().getGroupCode());
        if(m!=null){
            String ux=Util.getSubUtil1(groupMsg.getMsg(), "image=\\{(.*?)\\}.mirai,file");;
            if(m.getMsg().equals(groupMsg.getMsg()))
                return true;
            else if(ux!=null&&m.getMsg().contains(ux)){
                return true;
            }
        }
        return  false;
    }
    /**
     * 设置是否复读？
     * @param groupMsg
     */
    public static boolean addMsg(GroupMsg groupMsg){
        if(groupMsg.getMsg().equals(preStr)){
            return false;
        }
        if(qun.get(groupMsg.getGroupInfo().getGroupCode())!=null){
            qun.replace(groupMsg.getGroupInfo().getGroupCode(),groupMsg);
        }else{
            qun.put(groupMsg.getGroupInfo().getGroupCode(),groupMsg);
        }
        return false;
    }

    public static void sendMsg(MsgSender msgSender,GroupMsg groupMsg, String s, String s1) {
        cloerMsg(groupMsg);
        msgSender.SENDER.sendGroupMsg(s,s1);
    }

    /**
     * 不带小尾巴 false
     * @param msgSender
     * @param groupMsg
     * @param s
     * @param flag
     */
    public static void sendMsg(MsgSender msgSender,GroupMsg groupMsg, String s, boolean flag) {
        cloerMsg(groupMsg);
        if(flag){
            sendMsg(msgSender,groupMsg,s);
            return;
        }
        System.out.println(s);
        msgSender.SENDER.sendGroupMsg(groupMsg,s);
    }
    public String getImageOutPath(String name,String qq){
//        System.out.println(BotConfig.getJarPath());
        return BotConfig.getJarPath() + "/myback/img/"+name+qq+".png";
    }
    public boolean HasImageOut(String name,String qq){
        return !hasFile(name, qq);
    }
    boolean hasFile(String name,String qq){
        File file = new File(getImageOutPath(name, qq));
        if (file == null||!file.exists())
            return false;
        return true;
    }
    public String getImageMsg(String name,String qq){
        return getImgCAT(getImageOutPath(name, qq));
    }


    public String imageByByteArray(byte[] toByteArray, String qq, String name) {
        FileOutputStream fileOutputStream = null;
        String fileName = getImageOutPath(name,qq);
        try {
            fileOutputStream = new FileOutputStream(fileName);
            fileOutputStream.write(toByteArray);
            return getImgCAT(fileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "图片生成失败！";
    }

    public String text(String s) {
        return s;
    }
}
